<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="维修保养记录ID" prop="recordId">
        <el-input v-model="form.recordId"></el-input>
      </el-form-item>
      <el-form-item label="收费金额" prop="amount">
        <el-input v-model="form.amount"></el-input>
      </el-form-item>
      <el-form-item label="付款方式" prop="paymentMethod">
        <el-select v-model="form.paymentMethod">
          <el-option label="现金" value="cash"></el-option>
          <el-option label="微信支付" value="wechat"></el-option>
          <el-option label="支付宝" value="alipay"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实际付款金额" prop="paymentAmount">
        <el-input v-model="form.paymentAmount"></el-input>
      </el-form-item>
      <el-form-item label="收费时间" prop="chargeTime">
        <el-date-picker type="datetime" v-model="form.chargeTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="所属门店ID" prop="storeId">
        <el-input v-model="form.storeId"></el-input>
      </el-form-item>
      <el-form-item label="收费单据描述" prop="des">
        <el-input v-model="form.des"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker type="datetime" v-model="form.createTime"></el-date-picker>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { save, modify } from '../../../api/chargeBill'
import { ElMessage } from 'element-plus'

const form = ref({
  id: '',
  recordId: '',
  amount: '',
  paymentMethod: '',
  paymentAmount: '',
  chargeTime: '',
  storeId: '',
  des: '',
  createTime: ''
})

const props = defineProps({
  // 新增汽车品牌标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    // console.log(props.dialogRowValue)
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  recordId: { required: true, message: '请输入维修保养记录ID', trigger: 'blur' },
  amount: { required: true, message: '请输入收费金额', trigger: 'blur' },
  paymentMethod: { required: true, message: '请选择付款方式', trigger: 'blur' },
  paymentAmount: { required: true, message: '请输入实际付款金额', trigger: 'blur' },
  chargeTime: { required: true, message: '请选择收费时间', trigger: 'blur' },
  storeId: { required: true, message: '请输入所属门店ID', trigger: 'blur' },
  des: { required: false },
  createTime: { required: false }
})

const emits = defineEmits(['update:modelValue', 'query'])

// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确认
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加' ? await save(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
